package ynu.edu.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ynu.edu.common.service.LogicService;
import ynu.edu.dao.IItemDao;
import ynu.edu.dao.IShopDao;
import ynu.edu.utils.UpdateTool;
import ynu.edu.dao.IBusinessDao;

import ynu.edu.entity.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BusinessService extends LogicService<IBusinessDao, Business,Long> {
    public BusinessService(@Autowired IBusinessDao lr) {
        super(lr);
    }

    @Resource
    private IShopDao shopDao;

    @Resource
    private IItemDao itemDao;

    protected Business getCurrentBusiness(User user){
        Business business = new Business();
        // 复制user中不为空的字段到business中
        UpdateTool.copyNullProperties(user, business);
        return business;
    }

    public Shop createShop(User user,Shop shop){
        Business business = getCurrentBusiness(user);
        business.createShop(shop);
        getDAO().save(business);
        //查询最后一个shop，包含了id
        return shopDao.findShopsByBusiness(business).get(business.getShops().size()-1);
    }

    public Item createItem(User user, Item item){
        Business business = getCurrentBusiness(user);
        business.createItem(item);
        getDAO().save(business);
        return itemDao.findItemsByBusiness(business).get(business.getItems().size()-1);
    }

    public List<Shop> listMyShops(User user){
        Business business = getCurrentBusiness(user);
        return shopDao.findShopsByBusiness(business);
    }

    public List<Item> listMyItems(User user){
        Business business = getCurrentBusiness(user);
        return itemDao.findItemsByBusiness(business);
    }
}
